<template>
  <el-dialog
    class="assign-role"
    title="分配角色"
    :visible="showRoleDialog"
    :close-on-click-modal="false"
    @open="openDialog"
    @close="closeDialog"
  >
    <!-- 这里准备复选框 -->
    <el-checkbox-group v-model="roleIds" v-loading="isLoading">
      <el-checkbox
        v-for="item in roleList"
        :key="item.id"
        :label="item.id"
      >{{ item.name }}
      </el-checkbox>
    </el-checkbox-group>
    <template #footer>
      <el-button
        type="primary"
        size="small"
        @click="assignRole"
      >确定</el-button>
      <el-button size="small" @click="closeDialog">取消</el-button>
    </template>
  </el-dialog>
</template>

<script>
import { getRoleListApi } from '@/api/setting'
import { getBaseUserInfoApi } from '@/api/user'
import { assignRolesApi } from '@/api/employees'
export default {
  props: {
    showRoleDialog: {
      type: Boolean,
      default: false
    },
    // 用户的id 用来查询当前用户的角色信息
    userId: {
      type: String,
      default: null
    }
  },
  data() {
    return {
      roleList: [],
      roleIds: [],
      isLoading: false
    }
  },
  methods: {
    async assignRole() {
      await assignRolesApi({ id: this.userId, roleIds: this.roleIds })
      this.closeDialog()
      this.$message.success('恭喜，角色分配成功')
    },
    closeDialog() {
      this.$emit('update:showRoleDialog', false)
    },
    // async openDialog() {
    //   this.isLoading = true
    //   await this.getRoleList()
    //   await this.getBaseUserInfo()
    //   this.isLoading = false
    // },
    openDialog() {
      this.isLoading = true
      Promise.all([this.getRoleList(), this.getBaseUserInfo()]).then(() => {
        this.isLoading = false
      })
    },
    /**
     * 获取列表数据
     */
    async getRoleList() {
      const {
        data: { rows }
      } = await getRoleListApi(1, 9999)
      this.roleList = rows
    },
    async getBaseUserInfo() {
      const {
        data: { roleIds }
      } = await getBaseUserInfoApi(this.userId)
      this.roleIds = roleIds
    }
  }
}
</script>
<style lang="scss" scoped>
.assign-role {
  ::v-deep {
    .el-checkbox {
      font-size: 30px;
    }
  }
}
</style>
